import {
  defaultEditorConfig,
  defaultToolbarConfig,
  simpleToolbarConfig,
  defaultUploadConfig,
  defaultUploadImageConfig,
  defaultUploadVideoConfig,
  defaultUploadFileConfig
} from '../defaultConfig'

const useConfigMerge = (props) => {
  // 工具栏配置
  const toolBarConfigProps = computed(() => {
    const { toolBarConfig, mode } = props
    const config =
      mode === 'default' ? defaultToolbarConfig : simpleToolbarConfig
    return typeof toolBarConfig === 'function'
      ? toolBarConfig({ ...config })
      : toolBarConfig || config
  })

  // 编辑器配置的合并
  const editorConfigProps = computed(() => {
    const MENU_CONF = props.editorConfig.MENU_CONF || {}
    const hoverbarKeys = {
      ...defaultEditorConfig.hoverbarKeys,
      ...props.editorConfig.hoverbarKeys
    }

    MENU_CONF['uploadImage'] = mergeFn({
      ...defaultUploadConfig,
      ...props.defaultUploadConfig,
      ...defaultUploadImageConfig,
      ...props.uploadImageConfig
    })
    MENU_CONF['uploadVideo'] = mergeFn({
      ...defaultUploadConfig,
      ...props.defaultUploadConfig,
      ...defaultUploadVideoConfig,
      ...props.uploadVideoConfig
    })
    MENU_CONF['uploadAttachment'] = mergeFn({
      ...defaultUploadConfig,
      ...props.defaultUploadConfig,
      ...defaultUploadFileConfig,
      ...props.uploadFileConfig
    })
    const result = {
      ...defaultEditorConfig,
      ...props.editorConfig,
      MENU_CONF,
      hoverbarKeys
    }

    return result
  })

  const mergeFn = (config) => {
    if (config.uploadMode === 'custom') {
      return {
        ...config,
        customUpload: (file: File, insertFn) =>
          config.customUpload(file, insertFn, config)
      }
    } else return config
  }

  return {
    editorConfigProps,
    toolBarConfigProps
  }
}

export default useConfigMerge
